package cate.game.role.show.msg;

import cate.common.table.show.row.HeadFrameDevRow;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBBlog;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.show.frame.HeadFrame;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

@ClientMsg(title = "头像框升级 の 请求")
public class HeadFrameLevelUpReq implements MsgInput {


    @NoteField("头像框id")
    public int tid;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        EcResult r = settle(cc.getRole());
        game.notice.message(cc.getRole(), r,true);
    }

    private EcResult settle(Role role) {
        EcResult r = new EcResult();
        HeadFrame item = role.getShow().headFrame.getItem(tid);
        if (item == null) {
            return r.fail("头像框不存在");
        }
        HeadFrameDevRow row = role.getGame().table.blog.headFrameDev.findRow(item.tid, item.level);
        if (row == null) {
            return r.fail("配置不存在");
        }
        if (StringUtils.isBlank(row.levelUpCost)) {
            return r.fail("配置不存在");
        }
        MixRes consume = new MixRes(row.levelUpCost);
        r = consume.consume(role);
        if (!r.ok()) {
            return r;
        }
        item.level++;
        role.sendNow(new HeadFrameLevelUpResp(item));
        role.getGame().notice.message(role, "升级成功");
        return r.success();
    }


    @Override
    public MsgConfig conf() {
        return MsgIdBBlog.HEAD_FRAME_LEVEL_UP_REQ;
    }
}
